package com.group11.studentm.view;

import com.group11.studentm.domain.Sdept;
import com.group11.studentm.domain.Student;
import com.group11.studentm.service.AdminService;
import com.group11.studentm.utils.ValidationUtil;

import java.util.List;
import java.util.Scanner;

/**
 * @date 2022/12/06/19:32
 * @since 17
 * 录入信息
 */
public class ViewAdmin {
    Scanner scanner = new Scanner(System.in);
    AdminService adminService = new AdminService();

    /**
     * 管理主界面
     */
    public void mainView() {
        while (true) {
            System.out.println("========管理员界面=========");
            System.out.println("添加新同学或老师[1]");
            System.out.println("添加新的院系或课程[2]");
            System.out.println("修改/删除人员信息[3]");
            System.out.println("退出登录[0]");
            int option = scanner.nextInt();
            switch (option) {
                case 1:
                    add();
                    break;
                case 2:
                    addApartmentOrCourse();
                    break;
                case 3:
                    UpdateAndDeleteBySno();
                    break;
                case 0:
                    System.out.println("正在退出管理页面...");
                    return;
                default:
                    System.out.println("输入有误！");
                    break;
            }
        }

    }

    /**
     * 增加一条记录student表
     */
    public boolean add() {
        System.out.println("-----------增加-----------");
        System.out.println("身份[老师/学生] 院系 学号 姓名 性别 年龄");
        System.out.print("身份：");
        String selectedItem = scanner.next();
        // 查询数据库列出可选择的院系
        List<Sdept> sdeptList = adminService.findAllSName();
        System.out.print("可供选择的院系：");
        for (Sdept sdept : sdeptList) {
            System.out.print(sdept.getSdept_name() + " ");
        }
        System.out.print("\n院系：");
        String sdeptName = scanner.next();
        System.out.print("学号：");
        String sno = scanner.next();
        System.out.print("姓名：");
        String name = scanner.next();
        System.out.print("性别：");
        String sex = scanner.next();
        System.out.print("年龄：");
        String age = scanner.next();

        boolean flag = false;
        if (sno.length() == 0 || name.length() == 0 || sex.length() == 0 || "".equals(age)) {
            System.out.println("信息部分为空无法提交");
            return false;
        } else if (!ValidationUtil.checkSno(sno)) {
            System.out.println("工(学)号输入有误");
            return false;
        } else if (!ValidationUtil.checkName(name)) {
            System.out.println("请输入正确的中文名字2-6位");
            return false;
        } else if (!ValidationUtil.checkSex(sex)) {
            System.out.println("请输入正确的性别");
            return false;
        } else if (!ValidationUtil.checkAge(age)) {
            System.out.println("请输入正确的年龄(1-120岁)");
            return false;
        } else {

            StringBuffer buffer = new StringBuffer(sno);
            String s = buffer.substring(0, 4);
            if ("学生".equals(selectedItem)) {
                if (!"2005".equals(s)) {
                    flag = true;
                }
            } else if ("老师".equals(selectedItem)) {
                if (!"1005".equals(s)) {
                    flag = true;
                }
            }
            if (flag) {
                System.out.println("工(学)号与身份不匹配");
            } else {
                // 成功返回1，失败返回0
                if (adminService.addPerson(sno, name, sex, Byte.parseByte(age), sdeptName)) {
                    System.out.println("添加成功");
                } else {
                    System.out.println("抱歉,添加失败!");
                }
            }
        }

        return true;
    }

    /**
     * 增加
     */
    public boolean addApartmentOrCourse() {
        System.out.println("-------添加部门或学科-------");
        System.out.print("添加院系[1]\t\t添加学科[2]");
        int option = scanner.nextInt();
        switch (option) {
            case 1:
                System.out.print("院系：");
                String department = scanner.next();
                if (!ValidationUtil.checkSdept(department)) {
                    System.out.println("请输入正确的学院名");
                    return false;
                } else {
                    if (adminService.addSdept(department)) {
                        System.out.println("添加部门成功");
                        return true;
                    } else System.out.println("添加失败");
                }
                break;
            case 2:
                System.out.print("课程名：");
                String cname = scanner.next();
                System.out.print("课程学分：");
                String credit = scanner.next();

                if (!ValidationUtil.checkCname(cname)) {
                    System.out.println("请输入正确的课程名");
                    return false;
                } else {
                    if (!ValidationUtil.checkCredit(credit)) {
                        System.out.println("请输入正确的学分(1-6)");
                        return false;
                    } else {
                        if (adminService.addCourse(cname, Byte.parseByte(credit))) {
                            System.out.println("课程添加成功");
                        }
                    }
                }
                break;
        }
        return true;
    }

    /**
     * 更新学生数据 删除学生数据
     */
    public boolean UpdateAndDeleteBySno() {
        System.out.println("---------更新和删除--------");
        System.out.print("输入学号：");
        String sno = scanner.next();
        while (!ValidationUtil.checkSno(sno.trim()) || !adminService.checkSno(sno)){
            System.out.println("学号不存在");
            System.out.print("输入学号：");
            sno = scanner.next();
        }

        System.out.println("更新[1]\t删除[2]");
        int option = scanner.nextInt();

        switch (option) {
            /*修改*/
            case 1:
                while (!updateView(sno)) ;
                break;
            /*删除*/
            case 2:
                if (adminService.delPerson(sno)) {
                    System.out.println("删除成功");
                } else
                    return false;
                break;
            default:
                System.out.println("输入有误!");
                break;
        }
        return true;
    }

    
    public boolean updateView(String sno) {
        Student stu = null;
        //根据学号查询信息，封装到javaBean里面，然后存储到对应的信息框上
        stu = adminService.findBySno(sno);
        String deptName = adminService.findOneDeptName(stu.getSdept_id()); // 根据id 转 院系名
        List<Sdept> sdeptList = adminService.findAllSName(); // 查询所有院系
        System.out.println("--------------修改信息----------------");
        System.out.println("个人信息："
                + stu.getSname() + " "
                + stu.getSsex() + " "
                + stu.getSage() + " "
                + deptName);
        /* 重复验证，直到值没有错误
        校验姓名*/
        System.out.print("姓名：");
        String sname = scanner.next();
        while (!ValidationUtil.checkCname(sname.trim())) {
            System.out.println("输入有误，重新输入");
            System.out.print("姓名：");
            sname = scanner.next();
        }
        /*校验性别*/
        System.out.print("性别：");
        String ssex = scanner.next();
        while (!ValidationUtil.checkSex(ssex.trim())) {
            System.out.println("输入有误，重新输入");
            System.out.print("性别：");
            ssex = scanner.next();
        }
        /*校验年龄*/
        System.out.print("年龄：");
        String sage = scanner.next();
        while (!ValidationUtil.checkAge(sage.trim())) {
            System.out.println("输入有误，重新输入");
            System.out.print("年龄：");
            sage = scanner.next();
        }
        System.out.print("可选择的学院：");
        for (Sdept dept : sdeptList) {
            System.out.print(dept.getSdept_name() + " ");
        }
        /*校验学院*/
        System.out.print("\n学院：");
        String sdept = scanner.next();
        while (!ValidationUtil.checkSdept(sdept.trim())) {
            System.out.println("输入有误，重新输入");
            System.out.print("\n学院：");
            sdept = scanner.next();
        }
        //获得信息赋值给stu实体类，然后插入数据
        stu.setSname(sname);
        stu.setSsex(ssex);
        stu.setSage(Byte.parseByte(sage.trim()));
        stu.setSdept_id(adminService.findSID(sdept));
        // 执行更新数据
        if (adminService.updatePerson(stu)) {
            // 修改成功，返回管理员页面
            System.out.println("修改成功！");
            return true;
        } else {
            // 修改失败，重新修改
            System.out.println("修改失败！");
            return false;
        }
    }
}
